package jwdt.wizards;

import java.sql.Connection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jwdt.common.Constant;
import jwdt.popup.actions.DevToolsAction;
import jwdt.utils.ConnectionUtils;
import jwdt.vo.DevToolsVo;
import jwdt.vo.InitProDataSourceVo;
import jwdt.vo.ProjectFileTypeVo;

import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionDialog;

public class DevToolsWizardPage_1 extends WizardPage {
	
	private ISelection selection;
	
	private DevToolsVo devToolsVo;
	
	public DevToolsVo getDevToolsVo() {
		return devToolsVo;
	}

	private InitProDataSourceVo curInitProDataSourceVo;
	private Map<String,ProjectFileTypeVo> tableFileMap;
	
	private Text project_Text;
	private Combo javaSourcesPath_Combo;
	private Text package_Text;
	private Combo dataSource_Combo;
	private Combo dataBaseType_Combo;
	private Text driverClassText;
	private Text databaseUrlText;
	private Text usernameText;
	private Text passwordText;
	private TabFolder dataBaseTabFolder;
	private TabFolder tabFolder;
	private TabItem dataSourceInfotabItem;
	private TabItem tableListTabItem;
	private TabItem tabItemJdbc;
	private TabItem tabItemJndi; 
	private Text jndiNameText;
	private Button defaultDataSource;
	private Button refreshBtn;
	private Button selectAllBtn;
	private Button selectNoneBtn;
	private Table table;	
	private Text tablePattern_Text;
	private Button packageBtn;
	
	private String[] header = {"tableName",
			Constant.PROJECT_FILE_TYPE_PAGE,
			Constant.PROJECT_FILE_TYPE_WEB,
			Constant.PROJECT_FILE_TYPE_SERVICE,
			Constant.PROJECT_FILE_TYPE_DAO,
			Constant.PROJECT_FILE_TYPE_ORM};
	
	//事件
	ModifyListener modifyListener = new ModifyListener() {
		public void modifyText(ModifyEvent e) {
			//Text
			if(e.getSource() instanceof Text){
				pageAsVo(e.getSource());	
			}	
			//validate
			valCanFinish();
		}
	};
	SelectionListener selectionListener = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			//Table
			if(e.getSource() instanceof Table){				
				pageAsVo(e.getSource());				
			}
			//Button
			if(e.getSource() instanceof Button){
				Button btn = (Button)e.getSource();				
				//packageBrowse
				if("packageBrowse".equals(btn.getData("name"))){
					try {
						String srcPath="/"+DevToolsAction.projectProperties.getProjectName()+"/"+javaSourcesPath_Combo.getText();
						IPackageFragmentRoot root = DevToolsAction.javaProject.findPackageFragmentRoot(new Path(srcPath));
						SelectionDialog sd = JavaUI.createPackageDialog(null,root);
						sd.open();
						Object[] objects = sd.getResult();
						if (null != objects && objects.length > 0) {
							IPackageFragment pf = (IPackageFragment) objects[0];
							package_Text.setText(pf.getElementName());
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}				
				}
				//reflesh
				if("reflesh".equals(btn.getData("name"))){
					initTableList();
					tableAsVo();
				}
				//selectAll
				if("selectAll".equals(btn.getData("name"))){
					TableItem[] tableItems = table.getItems();
					for(TableItem tableItem : tableItems){
						tableItem.setChecked(true);
					}
					tableAsVo();
				}
				//selectNone
				if("selectNone".equals(btn.getData("name"))){
					TableItem[] tableItems = table.getItems();
					for(TableItem tableItem : tableItems){
						tableItem.setChecked(false);
					}
					tableAsVo();
				}
			}
			
			//TabFolder
			if(e.getSource() instanceof TabFolder){
				TabFolder tabFolder = (TabFolder)e.getSource();
				if("dataSourceTypeTabFolder".equals(tabFolder.getData("name"))){
					pageAsVo(e.getSource());	
				}				
			}	
			//Combo
			if(e.getSource() instanceof Combo){
				Combo combo = (Combo) e.getSource();
				//table editor Combo
				if(combo.getData("type") != null){
					tableFileMapChange(combo);						
				}												
				//javaSourcesPath
				if("javaSourcesPath".equals(combo.getData("name"))){
					pageAsVo(e.getSource());
				}
				//dataBaseType
				if("dataBaseType".equals(combo.getData("name"))){
					pageAsVo(e.getSource());
				}
				//dataSourceName
				if("dataSourceName".equals(combo.getData("name"))){
					int index = combo.getSelectionIndex();
					String str = combo.getItems()[index];
					pageAsVo(e.getSource());
					if("".equals(str)){
						//重置default
						defaultDataSource.setSelection(false);
						curInitProDataSourceVo.setDefault(false);
						//是否可以编辑dataSourcePage
						dataSourcePageRule(str);
					}else{
						InitProDataSourceVo initProDataSourceVo = DevToolsAction.projectProperties.getInitProDataSourceVoMap().get(str);
						voAsPage(initProDataSourceVo);		
					}
				}
			}
			//validate
			valCanFinish();
		}
	};
	
	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public DevToolsWizardPage_1(ISelection selection) {
		super("wizardPage");
		setTitle("DevTools");
		setDescription("for Dynamic Web Project(2.4/2.5) - DevTools.");
		
		this.selection = selection;
				
		curInitProDataSourceVo = new InitProDataSourceVo();
		curInitProDataSourceVo.setDefault(false);
		curInitProDataSourceVo.setDataSourceType(Constant.DATASOURCE_TYPE_JDBC);
		
		devToolsVo = new DevToolsVo();
		devToolsVo.setInitProDataSourceVo(curInitProDataSourceVo);
		devToolsVo.setJavaSourcesPath(DevToolsAction.projectProperties.getJavaSourcesPath()[0]);
		
		tableFileMap = new HashMap<String,ProjectFileTypeVo>();
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		
		GridLayout container_layout = new GridLayout();
		container_layout.numColumns = 1;
		container_layout.makeColumnsEqualWidth = true;
		container_layout.verticalSpacing=9;
		container.setLayout(container_layout);				
		
		/**
		 * Project Info
		 */		
		Group Project = new Group(container,SWT.NONE);
		Project.setText("Project Info");
		
		GridLayout Project_layout = new GridLayout();
		Project_layout.numColumns = 2;
		Project_layout.makeColumnsEqualWidth = false;
		Project_layout.verticalSpacing=9;
		Project.setLayout(Project_layout);		
		Project.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//Project Name
		Label project_Name = new Label(Project, SWT.NONE);		
		project_Name.setText("Project Name：");
		project_Name.setLayoutData(new GridData(GridData.BEGINNING));
		
		project_Text = new Text(Project,SWT.BORDER | SWT.SINGLE);
		project_Text.setData("name", "projectName");
		project_Text.setEditable(false);
		project_Text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		project_Text.setText(DevToolsAction.projectProperties.getProjectName());

		/**
		 * DataBase Info
		 */
		Group DataBase = new Group(container,SWT.NONE);
		DataBase.setText("DataBase Info");
		
		GridLayout DataBase_layout = new GridLayout();
		DataBase_layout.numColumns = 1;
		DataBase_layout.makeColumnsEqualWidth = true;
		DataBase.setLayout(DataBase_layout);		
		DataBase.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		
		/**
		 * DataSource Info & Table List
		 */
		dataBaseTabFolder = new TabFolder(DataBase, SWT.TOP);
		dataBaseTabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
				
		//DataSource Info
		dataSourceInfotabItem = new TabItem(dataBaseTabFolder, SWT.NONE);
		dataSourceInfotabItem.setData("name", "dataSourceInfo");
		dataSourceInfotabItem.setText("DataSource Info");
			
		Composite DataSource = new Composite(dataBaseTabFolder,SWT.NONE);		
		GridLayout DataSource_layout = new GridLayout();
		DataSource_layout.numColumns = 1;
		DataSource_layout.makeColumnsEqualWidth = true;
		DataSource.setLayout(DataSource_layout);		
		DataSource.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//dataBaseType
		Composite dataBaseType = new Composite(DataSource, SWT.NONE);
		dataBaseType.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		GridLayout dataBaseType_layout = new GridLayout();
		dataBaseType_layout.numColumns = 2;
		dataBaseType_layout.makeColumnsEqualWidth = false;
		dataBaseType_layout.verticalSpacing=9;
		dataBaseType.setLayout(dataBaseType_layout);	
		
		//DataSource Name
		Label dataSource_Name = new Label(dataBaseType, SWT.NONE);		
		dataSource_Name.setText("DataSource Name：");
		dataSource_Name.setLayoutData(new GridData(GridData.BEGINNING));
		
		dataSource_Combo = new Combo(dataBaseType, SWT.DROP_DOWN | SWT.READ_ONLY);
		//注册事件
		dataSource_Combo.addSelectionListener(selectionListener);
		dataSource_Combo.setData("name", "dataSourceName");
		dataSource_Combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		String[] dataSource_Combo_Items = new String[DevToolsAction.projectProperties.getInitProDataSourceVoMap().size()+1];
		DevToolsAction.projectProperties.getInitProDataSourceVoMap().keySet().toArray(dataSource_Combo_Items);	
		dataSource_Combo_Items[dataSource_Combo_Items.length-1]="";
		Arrays.sort(dataSource_Combo_Items);
		dataSource_Combo.setItems(dataSource_Combo_Items);
		
		//DataBase Type
		Label dataBaseType_Name = new Label(dataBaseType, SWT.NONE);		
		dataBaseType_Name.setText("DataBase Type：");
		dataBaseType_Name.setLayoutData(new GridData(GridData.BEGINNING));
		
		dataBaseType_Combo = new Combo(dataBaseType, SWT.DROP_DOWN | SWT.READ_ONLY);
		//注册事件
		dataBaseType_Combo.addSelectionListener(selectionListener);
		dataBaseType_Combo.setData("name", "dataBaseType");
		dataBaseType_Combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dataBaseType_Combo.setItems(Constant.DB_TYPE);
		
		//Jdbc/Jndi
		tabFolder = new TabFolder(DataSource, SWT.TOP);
		//注册事件
		tabFolder.addSelectionListener(selectionListener);
		tabFolder.setData("name", "dataSourceTypeTabFolder");
		tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
				
		//Jdbc
		tabItemJdbc = new TabItem(tabFolder, SWT.NONE);
		tabItemJdbc.setData("name", Constant.DATASOURCE_TYPE_JDBC);
		tabItemJdbc.setText("Jdbc");
				
		Composite jdbcComposite = new Composite(tabFolder, SWT.NONE);
		jdbcComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		GridLayout jdbcComposite_layout = new GridLayout();
		jdbcComposite_layout.numColumns = 2;
		jdbcComposite_layout.makeColumnsEqualWidth = false;
		jdbcComposite_layout.verticalSpacing=9;
		jdbcComposite.setLayout(jdbcComposite_layout);
		
		//Driver Class
		Label driverClassLabel = new Label(jdbcComposite, SWT.NONE);		
		driverClassLabel.setText("Driver Class：");
		driverClassLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		driverClassText = new Text(jdbcComposite,SWT.BORDER | SWT.SINGLE);
		//注册事件
		driverClassText.addModifyListener(modifyListener);
		driverClassText.setData("name", "driverClass");
		driverClassText.setEditable(true);
		driverClassText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//Database Url Class
		Label databaseUrlLabel = new Label(jdbcComposite, SWT.NONE);		
		databaseUrlLabel.setText("Database Url：");
		databaseUrlLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		databaseUrlText = new Text(jdbcComposite,SWT.BORDER | SWT.SINGLE);
		//注册事件
		databaseUrlText.addModifyListener(modifyListener);
		databaseUrlText.setData("name", "dataBaseUrl");
		databaseUrlText.setEditable(true);
		databaseUrlText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//Username Class
		Label usernameLabel = new Label(jdbcComposite, SWT.NONE);		
		usernameLabel.setText("Username：");
		usernameLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		usernameText = new Text(jdbcComposite,SWT.BORDER | SWT.SINGLE);
		//注册事件
		usernameText.addModifyListener(modifyListener);
		usernameText.setData("name", "userName");
		usernameText.setEditable(true);
		usernameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//Password Class
		Label passwordLabel = new Label(jdbcComposite, SWT.NONE);		
		passwordLabel.setText("Password：");
		passwordLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		passwordText = new Text(jdbcComposite,SWT.BORDER | SWT.SINGLE);
		//注册事件
		passwordText.addModifyListener(modifyListener);
		passwordText.setData("name", "passWord");
		passwordText.setEditable(true);
		passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		tabItemJdbc.setControl(jdbcComposite);
		
		//Jndi
		tabItemJndi = new TabItem(tabFolder, SWT.NONE);
		tabItemJndi.setData("name", Constant.DATASOURCE_TYPE_JNDI);
		tabItemJndi.setText("Jndi");

		Composite jndiComposite = new Composite(tabFolder, SWT.NONE);
		jndiComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		GridLayout jndiComposite_layout = new GridLayout();
		jndiComposite_layout.numColumns = 2;
		jndiComposite_layout.makeColumnsEqualWidth = false;
		jndiComposite_layout.verticalSpacing=9;
		jndiComposite.setLayout(jndiComposite_layout);

		//jndiName
		Label jndiNameLabel = new Label(jndiComposite, SWT.NONE);		
		jndiNameLabel.setText("Jndi Name：");
		jndiNameLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		jndiNameText = new Text(jndiComposite,SWT.BORDER | SWT.SINGLE);
		//注册事件
		jndiNameText.addModifyListener(modifyListener);
		jndiNameText.setData("name", "jndiName");
		jndiNameText.setEditable(true);
		jndiNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		tabItemJndi.setControl(jndiComposite);
			
		//是否为默认数据源
		defaultDataSource = new Button(DataSource,SWT.CHECK);
		defaultDataSource.setData("name", "isDefault");
		defaultDataSource.setText("Default");
		defaultDataSource.setSelection(false);
		defaultDataSource.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		defaultDataSource.setEnabled(false);
		
		dataSourceInfotabItem.setControl(DataSource);
		
		//Table List
		tableListTabItem = new TabItem(dataBaseTabFolder, SWT.NONE);
		tableListTabItem.setData("name", "tableList");
		tableListTabItem.setText("Table List");
		
		Composite tableList = new Composite(dataBaseTabFolder,SWT.NONE);		
		
		GridLayout tableList_layout = new GridLayout();
		tableList_layout.numColumns = 2;
		tableList_layout.makeColumnsEqualWidth = false;
		tableList_layout.verticalSpacing=9;
		tableList.setLayout(tableList_layout);		
		tableList.setLayoutData(new GridData(GridData.FILL_BOTH));
			
		Composite btnLeft = new Composite(tableList, SWT.NONE);
		btnLeft.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		GridLayout btnLeft_layout = new GridLayout();
		btnLeft_layout.numColumns = 1;
		btnLeft_layout.makeColumnsEqualWidth = true;
		btnLeft.setLayout(btnLeft_layout);
		
		tablePattern_Text = new Text(btnLeft,SWT.BORDER | SWT.SINGLE);
		//注册事件
		tablePattern_Text.addModifyListener(modifyListener);
		tablePattern_Text.setData("name", "tablePattern");
		tablePattern_Text.setEditable(true);
		tablePattern_Text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		table = new Table(btnLeft, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK);
		//注册事件
		table.addSelectionListener(selectionListener);
		//设置属性
		table.setData("name", "tableList");
		table.setHeaderVisible(true);
		table.setLinesVisible(false);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));

		for(int i=0 ; i<header.length ; i++){
			if(i > 0){
				TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
				tableColumn.setText(header[i]);
				tableColumn.setResizable(false);
			}else{
				TableColumn tableColumn = new TableColumn(table, SWT.NONE);
				tableColumn.setText(header[i]);
				tableColumn.setResizable(true);
			}			
		}	
		
		for(int i=0 ; i<header.length ; i++){
			table.getColumn(i).pack();
		}
		
		Composite btns = new Composite(tableList, SWT.NONE);
		btns.setLayoutData(new GridData(GridData.FILL_VERTICAL | GridData.VERTICAL_ALIGN_BEGINNING));
		
		GridLayout btns_layout = new GridLayout();
		btns_layout.numColumns = 1;
		btns_layout.makeColumnsEqualWidth = true;
		btns_layout.verticalSpacing=9;
		btns.setLayout(btns_layout);
				
		refreshBtn = new Button(btns, SWT.NONE);
		//注册事件
		refreshBtn.addSelectionListener(selectionListener);
		refreshBtn.setData("name", "reflesh");
		refreshBtn.setText("Reflesh");
		refreshBtn.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL));
		
		selectAllBtn = new Button(btns, SWT.NONE);
		//注册事件
		selectAllBtn.addSelectionListener(selectionListener);
		selectAllBtn.setData("name", "selectAll");
		selectAllBtn.setText("Select All");
		selectAllBtn.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL));
		
		selectNoneBtn = new Button(btns, SWT.NONE);
		//注册事件
		selectNoneBtn.addSelectionListener(selectionListener);
		selectNoneBtn.setData("name", "selectNone");
		selectNoneBtn.setText("Select None");
		selectNoneBtn.setLayoutData(new GridData(SWT.CENTER | GridData.FILL_HORIZONTAL));
		
		tableListTabItem.setControl(tableList);
		
		/**
		 * package Info
		 */		
		Group packageInfo = new Group(container,SWT.NONE);
		packageInfo.setText("Package Info");
		
		GridLayout packageInfo_layout = new GridLayout();
		packageInfo_layout.numColumns = 3;
		packageInfo_layout.makeColumnsEqualWidth = false;
		packageInfo_layout.verticalSpacing=9;
		packageInfo.setLayout(packageInfo_layout);		
		packageInfo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//javaSourcesPath Name
		Label javaSourcesPath_Name = new Label(packageInfo, SWT.NONE);		
		javaSourcesPath_Name.setText("Sources：");
		javaSourcesPath_Name.setLayoutData(new GridData(GridData.BEGINNING));
		
		javaSourcesPath_Combo = new Combo(packageInfo, SWT.DROP_DOWN | SWT.READ_ONLY);
		//注册事件
		javaSourcesPath_Combo.addSelectionListener(selectionListener);
		javaSourcesPath_Combo.setData("name", "javaSourcesPath");
		javaSourcesPath_Combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));		
		javaSourcesPath_Combo.setItems(DevToolsAction.projectProperties.getJavaSourcesPath());
		javaSourcesPath_Combo.setText(DevToolsAction.projectProperties.getJavaSourcesPath()[0]);
		
		Label emptyLabel = new Label(packageInfo, SWT.NONE);	
		emptyLabel.setLayoutData(new GridData(GridData.BEGINNING));
		
		//package Name
		Label package_Name = new Label(packageInfo, SWT.NONE);		
		package_Name.setText("Package：");
		package_Name.setLayoutData(new GridData(GridData.BEGINNING));
		
		package_Text = new Text(packageInfo,SWT.BORDER | SWT.SINGLE);
		//注册事件
		package_Text.addModifyListener(modifyListener);
		package_Text.setData("name", "package");
		package_Text.setEditable(true);
		package_Text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		packageBtn = new Button(packageInfo, SWT.NONE);
		//注册事件
		packageBtn.addSelectionListener(selectionListener);
		packageBtn.setData("name", "packageBrowse");
		packageBtn.setText("Browse");
		packageBtn.setLayoutData(new GridData(SWT.CENTER));

		//初始化页面
		this.voAsPage(DevToolsAction.projectProperties.getDevtoolsInfo());
		
		setControl(container);
	}

	//填充页面
	private void voAsPage(DevToolsVo dtVo){
		if(dtVo.getJavaSourcesPath()!=null && dtVo.getJavaSourcesPath().length()>1){
			javaSourcesPath_Combo.setText(dtVo.getJavaSourcesPath());
			devToolsVo.setJavaSourcesPath(dtVo.getJavaSourcesPath());
		}
		
		package_Text.setText(dtVo.getPackageText());
		devToolsVo.setPackageText(dtVo.getPackageText());
		
		tablePattern_Text.setText(dtVo.getTablePattern());
		devToolsVo.setTablePattern(dtVo.getTablePattern());
		
		this.voAsPage(dtVo.getInitProDataSourceVo());
	}
	private void voAsPage(InitProDataSourceVo initProDataSourceVo) {
		dataSource_Combo.setText(initProDataSourceVo.getDataSourceName());
		curInitProDataSourceVo.setDataSourceName(initProDataSourceVo.getDataSourceName());
		dataBaseType_Combo.setText(initProDataSourceVo.getDataBaseType());
		curInitProDataSourceVo.setDataBaseType(initProDataSourceVo.getDataBaseType());
		driverClassText.setText(initProDataSourceVo.getDriverClass());
		curInitProDataSourceVo.setDriverClass(initProDataSourceVo.getDriverClass());
		databaseUrlText.setText(initProDataSourceVo.getDataBaseUrl());
		curInitProDataSourceVo.setDataBaseUrl(initProDataSourceVo.getDataBaseUrl());
		usernameText.setText(initProDataSourceVo.getUserName());
		curInitProDataSourceVo.setUserName(initProDataSourceVo.getUserName());
		passwordText.setText(initProDataSourceVo.getPassWord());
		curInitProDataSourceVo.setPassWord(initProDataSourceVo.getPassWord());
		jndiNameText.setText(initProDataSourceVo.getJndiName());
		curInitProDataSourceVo.setJndiName(initProDataSourceVo.getJndiName());
		if(initProDataSourceVo.isDefault()){
			defaultDataSource.setSelection(true);
			curInitProDataSourceVo.setDefault(true);
		}else{
			defaultDataSource.setSelection(false);
			curInitProDataSourceVo.setDefault(false);
		}						
		if(initProDataSourceVo.getDataSourceType().equals(Constant.DATASOURCE_TYPE_JDBC)){			
			tabFolder.setSelection(tabItemJdbc);	
			curInitProDataSourceVo.setDataSourceType(Constant.DATASOURCE_TYPE_JDBC);
		}
		if(initProDataSourceVo.getDataSourceType().equals(Constant.DATASOURCE_TYPE_JNDI)){
			tabFolder.setSelection(tabItemJndi);	
			curInitProDataSourceVo.setDataSourceType(Constant.DATASOURCE_TYPE_JNDI);
		}
		
		//根据DataSourceName判定是否可以编辑dataSourcePage
		dataSourcePageRule(initProDataSourceVo.getDataSourceName());
	}
	//是否可以编辑dataSourcePage
	private void dataSourcePageRule(String dataSourceName){
		if("".equals(dataSourceName)){
			dataBaseType_Combo.setEnabled(true);
			driverClassText.setEnabled(true);
			databaseUrlText.setEnabled(true);
			usernameText.setEnabled(true);
			passwordText.setEnabled(true);
			jndiNameText.setEnabled(true);
			tabFolder.setEnabled(true);
		}else{
			dataBaseType_Combo.setEnabled(false);
			driverClassText.setEnabled(false);
			databaseUrlText.setEnabled(false);
			usernameText.setEnabled(false);
			passwordText.setEnabled(false);
			jndiNameText.setEnabled(false);
			tabFolder.setEnabled(false);
		}
	}
	//获取Vo值
	private void pageAsVo(Object eventSource) {
		//Table
		if(eventSource instanceof Table){
			tableAsVo();
		}
		//Text
		if(eventSource instanceof Text){
			Text txt = (Text) eventSource;	
			if(curInitProDataSourceVo.getDataSourceType().equals(Constant.DATASOURCE_TYPE_JDBC)){
				if(txt.getData("name").equals("driverClass")){
					curInitProDataSourceVo.setDriverClass(txt.getText().trim());
				}
				if(txt.getData("name").equals("dataBaseUrl")){
					curInitProDataSourceVo.setDataBaseUrl(txt.getText().trim());
				}
				if(txt.getData("name").equals("userName")){
					curInitProDataSourceVo.setUserName(txt.getText().trim());
				}
				if(txt.getData("name").equals("passWord")){
					curInitProDataSourceVo.setPassWord(txt.getText().trim());
				}
			}
			if(curInitProDataSourceVo.getDataSourceType().equals(Constant.DATASOURCE_TYPE_JNDI)){				
				if(txt.getData("name").equals("jndiName")){
					curInitProDataSourceVo.setJndiName(txt.getText().trim());
				}
			}
			//package
			if(txt.getData("name").equals("package")){
				devToolsVo.setPackageText(txt.getText().trim());
			}
			//tablePattern
			if(txt.getData("name").equals("tablePattern")){
				devToolsVo.setTablePattern(txt.getText().trim());
			}
		}
		//TabFolder
		if(eventSource instanceof TabFolder){
			TabFolder tabFolder = (TabFolder) eventSource;
			if("dataSourceTypeTabFolder".equals(tabFolder.getData("name"))){
				int index = tabFolder.getSelectionIndex();
				TabItem tabItem = tabFolder.getItem(index);
				if(tabItem != null && tabItem.getData("name") != null){						
					curInitProDataSourceVo.setDataSourceType(tabItem.getData("name").toString());
				}
			}								 					
		}
		//Combo
		if(eventSource instanceof Combo){
			Combo combo = (Combo) eventSource;
			if(combo.getData("name").equals("dataBaseType")){
				curInitProDataSourceVo.setDataBaseType(combo.getText().trim());
			}
			if(combo.getData("name").equals("dataSourceName")){
				curInitProDataSourceVo.setDataSourceName(combo.getText().trim());
			}
			//javaSourcesPath
			if(combo.getData("name").equals("javaSourcesPath")){
				devToolsVo.setJavaSourcesPath(combo.getText().trim());
			}
		}
		//Button
		if(eventSource instanceof Button){
			Button btn = (Button)eventSource;
			if(btn.getData("name").equals("isDefault")){
				curInitProDataSourceVo.setDefault(!curInitProDataSourceVo.isDefault());
			}
		}
	}

	//tableAsVo
	private void tableAsVo() {	
		TableItem[] tableItems = table.getItems();
		devToolsVo.getTableFileMap().clear();
		for(TableItem tableItem : tableItems){
			if(tableItem.getChecked()){	
				devToolsVo.getTableFileMap().put(tableItem.getText(),tableFileMap.get(tableItem.getText()));
			}		
		}
	}
	
	//tableFileMapChange
	private void tableFileMapChange(Combo combo){
		for(String fileType : Constant.PROJECT_FILE_TYPES){
			if(fileType.equals(combo.getData("type"))){				
				if(fileType.equals(Constant.PROJECT_FILE_TYPE_PAGE)){
					if(combo.getText().equals(Constant.YES)){
						tableFileMap.get(combo.getData("name")).setPage(true);
					}
					if(combo.getText().equals(Constant.NO)){
						tableFileMap.get(combo.getData("name")).setPage(false);
					}
				}
				if(fileType.equals(Constant.PROJECT_FILE_TYPE_WEB)){
					if(combo.getText().equals(Constant.YES)){
						tableFileMap.get(combo.getData("name")).setWeb(true);
					}
					if(combo.getText().equals(Constant.NO)){
						tableFileMap.get(combo.getData("name")).setWeb(false);
					}
				}
				if(fileType.equals(Constant.PROJECT_FILE_TYPE_SERVICE)){
					if(combo.getText().equals(Constant.YES)){
						tableFileMap.get(combo.getData("name")).setService(true);
					}
					if(combo.getText().equals(Constant.NO)){
						tableFileMap.get(combo.getData("name")).setService(false);
					}
				}
				if(fileType.equals(Constant.PROJECT_FILE_TYPE_DAO)){
					if(combo.getText().equals(Constant.YES)){
						tableFileMap.get(combo.getData("name")).setDao(true);
					}
					if(combo.getText().equals(Constant.NO)){
						tableFileMap.get(combo.getData("name")).setDao(false);
					}
				}
				if(fileType.equals(Constant.PROJECT_FILE_TYPE_ORM)){
					if(combo.getText().equals(Constant.YES)){
						tableFileMap.get(combo.getData("name")).setOrm(true);
					}
					if(combo.getText().equals(Constant.NO)){
						tableFileMap.get(combo.getData("name")).setOrm(false);
					}
				}						
			}
		}		
	}
	
	//canFinish
	private void valCanFinish(){
		String validateStr = validatePage();
		if(validateStr == null){
			setPageComplete(true);			
		}else{
			setPageComplete(false);
		}		
		setErrorMessage(validateStr);
	}
	
	//验证
	private String validatePage(){
		String returnValue = null;

		if(curInitProDataSourceVo.getDataBaseType() == null || curInitProDataSourceVo.getDataBaseType().length() < 1){
			returnValue = "DataBase Type is empty.";			
			return returnValue;
		}
		if(curInitProDataSourceVo.getDataSourceType().equals(Constant.DATASOURCE_TYPE_JDBC)){
			if(curInitProDataSourceVo.getDataBaseUrl() == null || curInitProDataSourceVo.getDataBaseUrl().length() < 1){
				returnValue = "DataBase Url is empty.";				
				return returnValue;
			}
			if(curInitProDataSourceVo.getDriverClass() == null || curInitProDataSourceVo.getDriverClass().length() < 1){
				returnValue = "Driver Class is empty.";				
				return returnValue;
			}
			if(curInitProDataSourceVo.getUserName() == null || curInitProDataSourceVo.getUserName().length() < 1){
				returnValue = "Username is empty.";				
				return returnValue;
			}
			if(curInitProDataSourceVo.getPassWord() == null || curInitProDataSourceVo.getPassWord().length() < 1){
				returnValue = "Password is empty.";				
				return returnValue;
			}
		}else{
			if(curInitProDataSourceVo.getJndiName() == null || curInitProDataSourceVo.getJndiName().length() < 1){
				returnValue = "Jndi Name is empty.";				
				return returnValue;
			}
		}			
		if(devToolsVo.getPackageText() == null || devToolsVo.getPackageText().length() < 1){
			returnValue = "Package is empty.";				
			return returnValue;
		}
		if(devToolsVo.getTableFileMap().size() < 1){			
			returnValue = "select tables.";				
			return returnValue;
		}

		return returnValue;
	}
	
	private void initTableList() {		
		Connection con = null;
		try {
			//获取数据库连接		
			con = ConnectionUtils.getConnection(curInitProDataSourceVo.getDriverClass(), 
												curInitProDataSourceVo.getDataBaseUrl(), 
												curInitProDataSourceVo.getUserName(), 
												curInitProDataSourceVo.getPassWord(),
												DevToolsAction.javaProject);
			//得到指定连接数据库中表
			List<String> tables = ConnectionUtils.getTables(con, null, devToolsVo.getTablePattern());
						
			//刷新Table
			reloadTable(tables);
		} catch (Exception e) {			
			table.removeAll();
			MessageDialog.openError(null, "错误", "请检查数据库配置或项目中的驱动程序是否存在...");
		} finally {
			if (null != con) {
				try {
					con.close();
				} catch (Exception ex) {}				
			}
		}
	}
	
	//刷新Table
	private void reloadTable(List<String> tables) {
		table.removeAll();	
		tableFileMap.clear();
		for (String key : tables) {
			TableItem tableItem = new TableItem(table, SWT.CENTER);			
			tableItem.setText(new String[]{key,"","","","",""});
			
			//editor		
			for(int i = 0; i < header.length; i++){	
				if(i > 0){					
					final TableEditor tableEditor = new TableEditor(table);										
					final Combo comboYorN = new Combo(table, SWT.DROP_DOWN | SWT.READ_ONLY);
					comboYorN.addSelectionListener(selectionListener);					
					comboYorN.setData("name", key);
					comboYorN.setData("type", header[i]);
					comboYorN.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));		
					comboYorN.setItems(new String[]{Constant.YES,Constant.NO});
					comboYorN.setText(Constant.YES);
					tableEditor.grabHorizontal = true;
					tableEditor.horizontalAlignment = SWT.CENTER;
					tableEditor.setEditor(comboYorN, tableItem, i);
					comboYorN.addSelectionListener(selectionListener);
				}				
			}
			
			//vo
			tableFileMap.put(key, new ProjectFileTypeVo());
		}
		for (int i = 0; i < header.length; i++) {
			table.getColumn(i).pack();
		}
	}
}